package com.qingniao.hb.web.servlet;

import com.alibaba.druid.util.StringUtils;
import com.qingniao.hb.service.BookService;
import com.qingniao.hb.service.impl.BookServiceImpl;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xlzhang
 * @date 2023/4/17 14:42
 */
@Slf4j
@WebServlet("*.html")
public class HtmlPageServlet extends BaseServlet {

    private final BookService bookService = new BookServiceImpl();
    /**
     * 登录页面显示
     */
    public void loginPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 加载真正的login.html页面
        // 根据baseServlet中模板解析器配置的前缀和后缀，
        // 最终这个页面的物理视图(真实路径)就是 /WEB-INF/templates/login.html
        process("login",request,response);
    }




    /**
     * 管理主页显示
     */
    public void managerPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 通过模板框架调用
        process("manager",request,response);
    }


    /**
     * 图书管理页显示
     * 在这个方法中加载页面要显示的数据，就可以保证任何地方只要显示页面，就会自动去查询和绑定
     */
    public void bookManagerPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 调用封装的功能来转换数据
        final HashMap<String, Object> paramsMap = changeParams(request);
        log.debug("--findByPage: {}", paramsMap);
        // 将参数传递给图书业务层，查询数据
        // 需求：应该返回一个分页数据的对象给我，可以将分页数据封装到map
        // 分页数据：bookList, currentPage, pages, total
        Map<String,Object> pageData = bookService.findBooksByParams(paramsMap);
        // 将数据保存到session
        request.setAttribute("pageData",pageData);
        // 通过模板框架调用
        process("pages/bookManager",request,response);
    }

    /**
     * 订单管理页显示
     */
    public void orderManagerPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 通过模板框架调用
        process("pages/orderManager",request,response);
    }


    private HashMap<String, Object> changeParams(HttpServletRequest request) {
        // 创建一个map集合，用来保存接收到的数据
        final HashMap<String, Object> paramMap = new HashMap<>();
        // 接收查询表单数据
        final String title = request.getParameter("title");
        if (!StringUtils.isEmpty(title)){
            paramMap.put("title",title);
        }
        final String author = request.getParameter("author");
        if (!StringUtils.isEmpty(author)){
            paramMap.put("author",author);
        }
        final String price = request.getParameter("price");
        if (!StringUtils.isEmpty(price)){
            // 防止用户在前端乱输入或者非法输入垃圾数据
            // 当前try处理后，就只能是小数
            try {
                final double pv = Double.parseDouble(price);
                paramMap.put("price",pv);
            } catch (NumberFormatException e) {
                System.out.println("e.getMessage() = " + e.getMessage());
            }
        }
        final String sales = request.getParameter("sales");
        if (!StringUtils.isEmpty(sales)){
            try {
                final int iv = Integer.parseInt(sales);
                paramMap.put("sales",iv);
            } catch (NumberFormatException e) {
                System.out.println("e = " + e.getMessage());
            }
        }
        final String stock = request.getParameter("stock");
        if (!StringUtils.isEmpty(stock)){
            try {
                final int iv = Integer.parseInt(stock);
                paramMap.put("stock",iv);
            } catch (NumberFormatException e) {
                System.out.println("e = " + e.getMessage());
            }
        }
        final String page = request.getParameter("page");
        if (!StringUtils.isEmpty(page)){
            try {
                final int iv = Integer.parseInt(page);
                paramMap.put("page",iv);
            } catch (NumberFormatException e) {
                log.error("--前端传入的分页数据类型异常");
                paramMap.put("page",1);
            }
        }else {
            paramMap.put("page",1);
        }

        // 每页显示条数
        paramMap.put("limit",5);

        return paramMap;
    }
}
